package com.irun.pm.controller;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.irun.pm.database.DBHelper;
import com.irun.pm.interfaces.IDriverInfoController;
import com.irun.pm.mode.DriverInfo;

public class DriverInfoManager implements IDriverInfoController {
	private DBHelper dbHelper;

	public DriverInfoManager(Context context) {
		dbHelper = new DBHelper(context);
		try {
			dbHelper.createDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void creteDb() {
		try {
			dbHelper.createDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public List<DriverInfo> getDriverInfoList(int type) {
		List<DriverInfo> pList = new ArrayList<DriverInfo>();
		try {
			String strType = DBHelper.FIELD_TYPE + " = " + type;
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strType, null,
					null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				DriverInfo info = new DriverInfo();
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
				pList.add(info);
			}
			if (cursor != null){
				cursor.close();
				db.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return pList;
	}

	@Override
	public List<DriverInfo> queryDriverInfoByName(String name,int type) {
		List<DriverInfo> pList = new ArrayList<DriverInfo>();
		String strType = DBHelper.FIELD_NAME + " like '%" + name +"%' and "+DBHelper.FIELD_TYPE +" = "+type;
		try {
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strType,
					null, null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				DriverInfo info = new DriverInfo();
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
				pList.add(info);
			}
			if (cursor != null)
				cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return pList;
	}

	@Override
	public DriverInfo queryDriverInfoById(int id) {
		DriverInfo info = new DriverInfo();
		String strId = DBHelper.FIELD_ID + " = " + id;
		try {
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strId,
					null, null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
			}
			if (cursor != null){
				cursor.close();
				db.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return info;
	}

	@Override
	public DriverInfo queryDriverInfoByCard(String card,int type) {
		// TODO Auto-generated method stub
		DriverInfo info = null;
		String strId = DBHelper.FIELD_CARD + " like '%" + card +"%' and "+DBHelper.FIELD_TYPE +" = "+type;
		try {
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strId,
					null, null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				info = new DriverInfo();
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
			}
			if (cursor != null)
				cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return info;
	}

	@Override
	public long insertDriverInfo(DriverInfo driverInfo, int type) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try{
			ContentValues cv = new ContentValues();
//			cv.put(DBHelper.FIELD_ID, driverInfo.id);
			cv.put(DBHelper.FIELD_NAME, driverInfo.name);
			cv.put(DBHelper.FIELD_SEX, driverInfo.sex);
			cv.put(DBHelper.FIELD_CARD, driverInfo.card);
			cv.put(DBHelper.FIELD_AGE, driverInfo.age);
			cv.put(DBHelper.FIELD_CARD_NUMBER, driverInfo.cardNumber);
			cv.put(DBHelper.FIELD_ADDRESS, driverInfo.address);
			cv.put(DBHelper.FIELD_NATIVE, driverInfo.nativeAddress);
			cv.put(DBHelper.FIELD_DRIVEAGE, driverInfo.driverAge);
			cv.put(DBHelper.FIELD_PATH, driverInfo.photoPath);
			cv.put(DBHelper.FIELD_TYPE, type);
			cv.put(DBHelper.FIELD_DATE, driverInfo.date);
			cv.put(DBHelper.FIELD_RESULT, driverInfo.result);
			cv.put(DBHelper.FIELD_DEAL_PEOPLE, driverInfo.deal_people);
			cv.put(DBHelper.FIELD_NOTE, driverInfo.note);
			long row = db.insert(DBHelper.TABLE_DRIVERSINFO, null, cv);
			return row;
		}catch (Exception e) {
			// TODO: handle exception
			return -1;
		}finally{
			db.close();
		}
		
		
	}

	@Override
	public List<DriverInfo> queryDriverInfoByCardNumber(String cardNumber, int type) {
		// TODO Auto-generated method stub
		List<DriverInfo> pList = new ArrayList<DriverInfo>();
//		DriverInfo detail = new DriverInfo();
		String strId = DBHelper.FIELD_CARD_NUMBER + " like '%" + cardNumber +"%' and "+DBHelper.FIELD_TYPE +" = "+type;
		try {
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strId,
					null, null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				DriverInfo info = new DriverInfo();
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
				pList.add(info);
			}
			if (cursor != null)
				cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return pList;
	}

	@Override
	public List<DriverInfo> queryDriverInfoByCardNumberAndName(String name,
			String cardNumber, int type) {
		List<DriverInfo> list = new ArrayList<DriverInfo>();
		String strId = DBHelper.FIELD_CARD_NUMBER + " like '%" + cardNumber +"%' and "+ DBHelper.FIELD_NAME +" = '"+name+"' and "+DBHelper.FIELD_TYPE +" = "+type;
		try {
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strId,
					null, null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				DriverInfo info = new DriverInfo();
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
				list.add(info);
			}
			if (cursor != null)
				cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	@Override
	public DriverInfo queryDriverInfoByCardAndName(String name, String card,
			int type) {
		// TODO Auto-generated method stub
		DriverInfo detail = new DriverInfo();
		String strId = DBHelper.FIELD_CARD + " like '%" + card +"%' and "+ DBHelper.FIELD_NAME +" like '%"+name+"%' and "+DBHelper.FIELD_TYPE +" = "+type;
		try {
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query(DBHelper.TABLE_DRIVERSINFO, null, strId,
					null, null, null, " _id desc");
			if (cursor == null) {
				return null;
			}
			while (cursor.moveToNext()) {
				DriverInfo info = new DriverInfo();
				info.setId(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setSex(cursor.getString(2));
				info.setCard(cursor.getString(3));
				info.setAge(cursor.getInt(4));
				info.setCardNumber(cursor.getString(5));
				info.setAddress(cursor.getString(6));
				info.setNativeAddress(cursor.getString(7));
				info.setDriverAge(cursor.getString(8));
				info.setPhotoPath(cursor.getString(9));
				info.setType(cursor.getInt(10));
				info.setDate(cursor.getString(11));
				info.setResult(cursor.getString(12));
				info.setDeal_people(cursor.getString(13));
				info.setNote(cursor.getString(14));
			}
			if (cursor != null)
				cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return detail;
	}

	@Override
	public long updatePhotoPath(DriverInfo driverInfo, String path) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String strId = DBHelper.FIELD_ID + " = " + driverInfo.id +" and "+ DBHelper.FIELD_TYPE +" = "+driverInfo.type;
		try{
			ContentValues cv = new ContentValues();
			cv.put(DBHelper.FIELD_NAME, driverInfo.name);
			cv.put(DBHelper.FIELD_SEX, driverInfo.sex);
			cv.put(DBHelper.FIELD_CARD, driverInfo.card);
			cv.put(DBHelper.FIELD_AGE, driverInfo.age);
			cv.put(DBHelper.FIELD_CARD_NUMBER, driverInfo.cardNumber);
			cv.put(DBHelper.FIELD_ADDRESS, driverInfo.address);
			cv.put(DBHelper.FIELD_NATIVE, driverInfo.nativeAddress);
			cv.put(DBHelper.FIELD_DRIVEAGE, driverInfo.driverAge);
			cv.put(DBHelper.FIELD_PATH, path);
			cv.put(DBHelper.FIELD_DATE, driverInfo.date);
			cv.put(DBHelper.FIELD_RESULT, driverInfo.result);
			cv.put(DBHelper.FIELD_DEAL_PEOPLE, driverInfo.deal_people);
			cv.put(DBHelper.FIELD_NOTE, driverInfo.note);
			long row = db.update(DBHelper.TABLE_DRIVERSINFO, cv, strId, null);
			return row;
		}catch (Exception e) {
			return -1;
		}finally{
			db.close();
		}
	}


}
